﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BSCore
{
    public class Sprite:GameComponent
    {
        private int currentFrameIndex = 0;
        private float totalElapsed = 0f;

        public int ID { get; set; }

        public List<FrameSet> FrameSets { get; set; }

        public int CurrentFrameIndex
        {
            get { return currentFrameIndex; }
            set 
            {
                if (value < 0 || value >= FrameSets.Count)
                {
                    currentFrameIndex = 0;
                }
                else
                {
                    currentFrameIndex = value; 
                }
            }
        }
        
        public FrameSet CurrentFrameSet 
        { 
            get
            {
                return FrameSets[CurrentFrameIndex];
            }
        }


        public Sprite(BSGame game):base(game)
        {

        }
        /// <summary>
        /// Updates the current frame depending on current frame interval
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            totalElapsed += elapsed;
            int interval =CurrentFrameSet.Interval;
            if (totalElapsed >= interval)
            {
                CurrentFrameIndex++;
                // reset the total elapsed time.
                totalElapsed -= interval;
            }
 
            base.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 position)
        {
            CurrentFrameSet.Frame.Draw(spriteBatch, position);
        }




    }
}
